require 'crack/xml'
require 'net/http'
require 'uri'


class LotusNotes
  
  attr_accessor :host, :mail_file
  
  def initialize
    @mail_file = 'jkirk.nsf'
    @host = 'http://localhost:8070'
  end
  
  # Retrieves email from the Notes server.
  # Email is returned as an array of Message objects.
  def get_mail
    url = URI.parse(@host)
    res = Net::HTTP.start(url.host, url.port) {|http|
        http.get('/mail/' + @mail_file + '/($inbox)?readviewentries')
    }
    xml_data = Crack::XML.parse(res.body)
    return parse_xml_data(xml_data)
  end
  
  
  def parse_xml_data(xml_data)
    @results = []
    xml_data['viewentries']['viewentry'].each do |entry|
      message = Message.new      
      message.parse(entry)      
      @results.push message
    end
    return @results
  end
  
end